using Microsoft.SPOT;
using System.IO;
using Microsoft.SPOT.IO;
using System.Text;
using System;

namespace HouseControl
{
    class DataBase
    {

        public static VolumeInfo volume;  //variable to know the path of our SD Card, it's our path reader
        public static string rootPath;   //variable that store the root path 

        public static string imagePath;  //where to put the text that store the history 

        //handle the event when the sd card is entered 
        public static void RemovableMedia_Insert(object sender, MediaEventArgs e)
        {
            Debug.Print("insert");
            volume = e.Volume;    //bind the path reader to the new media inserted 
            rootPath = volume.RootDirectory; //take the root path of the media just inserted
            Debug.Print(rootPath.ToString());

            imagePath = Path.Combine(rootPath, "history.txt"); //put the full path that contain the database to this string variable 

        }

        
        //this is for the android user when he changes light 1, we store it in the database
        public static void android_user_add_light1_state_to_database()
        {

            // Create the file if it doesn't exists.
            if (!File.Exists(imagePath))
            {
                // Create the file.
                File.Create(imagePath);
            }

              try
            {
                using (System.IO.TextWriter textWriter = new StreamWriter(imagePath.ToString(), true))//the file and the mode append is true
                {

                    textWriter.Write("\r\n" + DateTime.Now.ToString() + "   " + " Android User " + "   " + " Light 1 " + "   " + Program.stateLed.ToString());//write the status
                }
            }

              catch (Exception ee)
              {
                  Debug.Print(ee.Message);
                  throw;

              }


        }
        //same as above but this is for the light 2
        public static void android_user_add_light2_state_to_database()
        {
            // Create the file if it doesn't exists.
            if (!File.Exists(imagePath))
            {
                // Create the file.
                File.Create(imagePath);
            }

               try
            {
                using (System.IO.TextWriter textWriter = new StreamWriter(imagePath.ToString(), true))
                {

                    textWriter.Write("\r\n" + DateTime.Now.ToString() + "   " + " Android User " + "   " + " Light 2 " + "   " + Program.stateLeds.ToString());
                }
            }

              catch (Exception ee)
              {
                  Debug.Print(ee.Message);
                  throw;

              }
        
        }
       
        //same as above but this is for the android user when he check the alarm on temperature
        public static void android_user_add_temperature_state_to_database()
        {
            // Create the file if it doesn't exists.
            if (!File.Exists(imagePath))
            {
                // Create the file.
                File.Create(imagePath);
            }

             try
            {
                using (System.IO.TextWriter textWriter = new StreamWriter(imagePath.ToString(), true))
                {

                    textWriter.Write("\r\n" + DateTime.Now.ToString() + "   " + " Android User " + "   " + " Door " + "   " + Program.gate.ToString());
                }
            }

              catch (Exception ee)
              {
                  Debug.Print(ee.Message);
                  throw;

              }   
            
        }

        //read the datatbase
        public static void read_database()
        {
            //Open the stream and read it back.
            using (FileStream fs = File.OpenRead(imagePath))
            {
                // Read the source file into a byte array.
                byte[] b = new byte[fs.Length];


                int numBytesToRead = (int)fs.Length;  //this is to know the number of bytes that we have to read  in order to avoid empty string 
                int numBytesRead = 0;                     //this is to know the length that we have already read !
                while (numBytesToRead > 0)             //if we still have something to read
                {
                    // Read may return anything from 0 to numBytesToRead
                    int n = fs.Read(b, numBytesRead, numBytesToRead);

                    // Break when the end of the file is reached
                    if (n == 0)
                        break;

                    numBytesRead += n;  // increase the number of bytes already read 
                    numBytesToRead -= n;  //decrease the number of byte to read 
                }
                numBytesToRead = b.Length;  // we have now the full length



                Program.read_database = new String(Encoding.UTF8.GetChars(b)).Split(new char[] { '\r', '\r' }); //read the data split it using the "go to new line"(\n) 


                //debug stuff
                for (int i = 0; i < Program.read_database.Length; i++)
                {
                    Debug.Print(Program.read_database[i].ToString());
                }
            }
        }
    }

}
